package com.yztstudy.Filter;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登录拦截器
 */

@Component
public class LoginProtectInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        // TODO 1.获取session对象
        HttpSession session = request.getSession();

        // TODO 2.判断user对象是否存在
        Object userInfo = session.getAttribute("userInfo");
        if (userInfo != null)
        {
            return true;
        }

        // TODO 3.管理员不存在,拦截
        else {
            response.sendRedirect(request.getContextPath() + "/index.html");
            return false;
        }
    }
}
